package com.share.admin.config;

import com.share.admin.constant.SysConstant;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2的接口配置
 *
 * @author ruoyi
 */
@Slf4j
@EnableSwagger2  //这个注解必须写否则有问题
@Configuration
public class SwaggerConfig {
    /**
     * 是否开启swagger
     */
    private boolean enabled = true;

    @Bean
    public Docket createRestApi() {
        List<Parameter> parameter = parameter();

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)   //是否开启swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.share.admin.web"))
                .paths(PathSelectors.any())

                .build().globalOperationParameters(parameter)

                .apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .version("9.0")
                        .build())
//
//                // swagger 全局header参数  --- 方式一
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());

                //swagger 全局header参数 --- 方式二

                .globalOperationParameters(parameter);
    }


    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
        apiKeyList.add(new ApiKey("header1", "token", In.HEADER.toValue()));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");

        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("header1", authorizationScopes));
        return securityReferences;
    }


    private List<Parameter> parameter() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("token").description("header中的token参数")
                .modelRef(new ModelRef("string")).defaultValue(SysConstant.SWAGGER_USER_ACCOUNT).parameterType("header").build(); //header中的token 字段

        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(ticketPar.build());
        return pars;
    }
}
